﻿using System;
using System.Web.UI;
using System.Net;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Evolution.Utilities;
using Evolution.Expression;

namespace Evolution.UI.WebParts
{
    [Serializable]
    public class ShowContentsAction : BaseAction
    {
        #region Constructors

        public ShowContentsAction()
        {

        }

        public ShowContentsAction(string contents, string url)
        {
            this.Contents = contents;
            this.Url = url;
        }

        public ShowContentsAction(string contents)
            : this(contents, String.Empty)
        {
        }

        #endregion

        #region Properties

        public string Contents { get; set; }

        public string Url { get; set; }

        public override string Caption
        {
            get { return "Show contents"; }
        }

        #endregion

        #region Methods

        public override void CreateInterface(ControlCollectionBuilder builder, PageActionEditor editor)
        {
            HiddenField contentsField = new HiddenField();
            contentsField.ID = "Evolution_Pawp_ContentsField";
            contentsField.Value = "";
            builder.AddControl(contentsField);

            builder.AddStaticText(@"<div class=""UserControlBody"" style=""margin-top: 5px"">To enter formatted text, graphics and ""$"" Expressions (but no ASPX tags), click <B>Rich Text Editor</B>. </div>");
            builder.AddStaticText(@"<div class=""UserControlGroup"" style=""padding-bottom: 5px"">");

            builder.AddStaticText(@"<input class=""UserButton"" style=""WIDTH: 165px; margin-top: 2px;"" onclick=""javascript:showModalDialog('{0}', document.getElementById('{1}'), 'dialogHeight:400px;dialogWidth:720px;help:no;status:no;resizable:yes');"" type=""button"" value=""Rich Text Editor..."" /> ",
                                  SPContext.Current.Web.Site.Url + "/_layouts/Evolution/code/htmledit.aspx?isRichText=true", contentsField.ClientID);

            builder.AddStaticText("</div>");

            builder.AddStaticText(@"<div class=""UserControlBody"" style=""margin-top: 5px"">To type HTML/ASPX source code and ""$"" Expressions, click <B>Source Editor</B>. </div>");
            builder.AddStaticText(@"<div class=""UserControlGroup"" style=""padding-bottom: 5px"">");

            builder.AddStaticText(@"<input class=""UserButton"" style=""WIDTH: 165px; margin-top: 2px;"" onclick=""javascript:showModalDialog('{0}', document.getElementById('{1}'), 'dialogHeight:400px;dialogWidth:720px;help:no;status:no;resizable:yes');"" type=""button"" value=""Source Editor..."" /> ",
                                  SPContext.Current.Web.Site.Url + "/_layouts/Evolution/code/htmledit.aspx", contentsField.ClientID);

            builder.AddStaticText("</div>");

            builder.AddStaticText(@"<div class=""UserControlBody"" style=""margin-top: 5px"">To link to a file (with HTML/ASPX source code and ""$"" Expressions), type a URL.</div>");
            builder.AddStaticText(@"<div class=""UserControlGroup"" style=""padding-bottom: 2px"">");

            TextBox url1 = new TextBox();
            url1.ID = "Evolution_Pawp_ContentsUrl";
            url1.CssClass = "UserInput";
            url1.ToolTip = "Url Link";
            url1.Style[HtmlTextWriterStyle.Width] = "175px";

            builder.AddTextBoxAndBuilder(url1, true);

            builder.AddStaticText("</div>");

            builder.AddErrorPlaceholder("Contents");
        }

        public override void Execute(Page page, PageActionWebPart part)
        {
            ExpressionInterpreter interp = PageActionWebPart.GetInterpreter(page, part);

            string source = String.Empty;
            Control control;
            if (!StringUtility.IsNullOrEmpty(this.Contents))
                source = this.Contents;
            else
            {
                WebClient client = new WebClient();
                client.Credentials = CredentialCache.DefaultNetworkCredentials;

                string sourceUrl = interp.ParseExpression(this.Url);

                source = client.DownloadString(sourceUrl);
            }
            source = interp.ParseExpression(source);
            control = page.ParseControl(source);
            part.Controls.Add(control);            
        }

        public override bool IsValidAction(PageActionWebPart part, out string errorMessage)
        {
            errorMessage = String.Empty;

            if (StringUtility.IsNullOrEmpty(this.Contents) && StringUtility.IsNullOrEmpty(this.Url))
                errorMessage = "No contents to display.";

            return true;
        }

        public override string GetDescription()
        {
            return "Show contents";
        }

        public override void ApplyChanges(PageActionEditor editor)
        {
            HiddenField contentsField = editor.FindControl("Evolution_Pawp_ContentsField") as HiddenField;
            TextBox url1 = editor.FindControl("Evolution_Pawp_ContentsUrl") as TextBox;

            if (StringUtility.IsNullOrEmpty(contentsField.Value) && StringUtility.IsNullOrEmpty(url1.Text))
                editor.RaiseError("Contents", "<br/>Specify the contents to display.", String.Format("The action '{0}' requires the indication of the contents to display.", this.Caption));

            this.Contents = contentsField.Value;
            this.Url = url1.Text;
        }

        public override void SyncChanges(PageActionEditor editor)
        {
            (editor.FindControl("Evolution_Pawp_ContentsField") as HiddenField).Value = this.Contents;
            (editor.FindControl("Evolution_Pawp_ContentsUrl") as TextBox).Text = this.Url;
        }

        #endregion
    }
}
